/**
 * 
 */
package com.effectiv.req1.controller;

import java.util.ArrayList;

import java.util.List;

import javax.validation.Valid;

import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.effectiv.req1.business.api.ProjectService;
import com.effectiv.req1.domain.Project;
import com.effectiv.req1.domain.User;


import com.effectiv.req1.security.SecurityUtil;

/**
 * @author Dhrubo
 * 
 */
@Controller
@Slf4j
public class ProjectController {


	@Autowired
	private ProjectService projectService;

	@RequestMapping(value = "/newproject")
	public final String create(Model model) {
		model.addAttribute(BeanUtils.instantiate(Project.class));
		return "newproject";
	}

	@RequestMapping(value = "/saveproject", method = RequestMethod.POST)
	public @ResponseBody AjaxResponse save(@Valid Project project, BindingResult result, AjaxResponse response) {
				
		if (!result.hasErrors()) {
			List<User> users = new ArrayList<User>();
			users.add(SecurityUtil.getUserDetails());
			project.setUsers(users);
			projectService.save(project);
			
			response.setResult(project);
		}
		else{
			response.markValidationFailure(result.getFieldErrors());
		}
		return response;
	}

	
	
	@RequestMapping(value = "/editproject")
	public final String view(@RequestParam("project") String id,
			Model model) {
		
		Project project = projectService.findOne(id);
		
		model.addAttribute(project);
		model.addAttribute("users", project.getUsers());
		model.addAttribute("actors", project.getActors());
		
		return "editproject";
	}
	
	@RequestMapping(value = "/updateproject", method = RequestMethod.POST)
	public @ResponseBody AjaxResponse update(@Valid Project project, BindingResult result, AjaxResponse response) {
		
		if (!result.hasErrors()) {
			projectService.update(project);
			response.setResult(project);
		}
		else{
			response.markValidationFailure(result.getFieldErrors());
		}
		return response;
	}

	@RequestMapping(value = "/listprojects")
	public final String list(Model model) {
		model.addAttribute("projects", projectService.findProjects());
		return "listprojects";
	}
	
	@RequestMapping(value = "/listprojectusers")
	public final String listProjectUsers(@RequestParam("projectId") String id, @RequestParam("selection") boolean selection,
			Model model) {
		Project project = projectService.findOne(id);
		model.addAttribute(project);
		model.addAttribute("users", project.getUsers());
		model.addAttribute("selection", selection);
		return "listusers";
	}
	
	@RequestMapping(value = "/updatemanager")
	public final String setManager(@RequestParam("project") String id, @RequestParam("user") String userId,
			Model model) {
		
		Project project = projectService.setManager(id, userId);
		
		model.addAttribute(project);
		model.addAttribute("users", project.getUsers());
		model.addAttribute("actors", project.getActors());
		
		return "editproject";
	}
}
